/**
 * Copyright 2014-2015 eyaomall.cn
 * All rights reserved.
 * 
 * @project
 * @author jipengfei
 * @version 1.0
 * @date 2016-03-08
 */
package com.eyaomall.wxmanage.service.impl;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eyaomall.wxmanage.common.constants.SmsConstants;
import com.eyaomall.wxmanage.common.error.BECode;
import com.eyaomall.wxmanage.common.helper.sms.sms163.Sms163Helper;
import com.eyaomall.wxmanage.dao.ShopDao;
import com.eyaomall.wxmanage.model.po.Sms;
import com.eyaomall.wxmanage.service.SmsService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.common.util.DateUtil;
import com.lycheeframework.core.exception.ErrorCodeException;
import com.lycheeframework.core.support.ServiceSupport;

/**
 * 
 * @author Flouny.Caesar
 *
 */
@Service
public class SmsServiceImpl extends ServiceSupport implements SmsService {
	
	@Autowired
	private ShopDao smsDao;

	/**
	 * 登陆短信
	 */
	public void login(String phone) {
		String code = getCode(phone, SmsConstants.LOGIN_TYPE);
		if(StringUtil.isEmpty(code))
			throw new ErrorCodeException(BECode.sms_301000);
		Sms163Helper.sendSms(3031151, phone, code);
//		SMSHelper.sendSMS(phone, getCode(phone, SmsConstants.LOGIN_TYPE).concat("（动态登录验证码）。工作人员不会向您索要，请勿向任何人泄露。"));
	}
	


	
	
	/**
	 * 注册短信
	 */
	public void register(String phone) {
		String code = getCode(phone, SmsConstants.REGISTER_TYPE);
		if(StringUtil.isEmpty(code))
			throw new ErrorCodeException(BECode.sms_301000);
		Sms163Helper.sendSms(3032111, phone, code);
//		SMSHelper.sendSMS(phone, "注册验证码 ".concat(getCode(phone, SmsConstants.REGISTER_TYPE)).concat("，有效期10分钟"));
	}
	/**
	 * 找回密码短信
	 */
	public void findPwd(String phone) {
		String code = getCode(phone, SmsConstants.BACK_PASSWD);
		if(StringUtil.isEmpty(code))
			throw new ErrorCodeException(BECode.sms_301000);
		Sms163Helper.sendSms(3032110, phone, code);
//		SMSHelper.sendSMS(phone, "找回密码验证码 ".concat(getCode(phone, SmsConstants.BACK_PASSWD)).concat("，有效期10分钟"));
	}

	/**
	 * 换绑手机短信
	 */
	public void inTie(String phone) {
		String code = getCode(phone, SmsConstants.CHANGE_PHONE);
		if(StringUtil.isEmpty(code))
			throw new ErrorCodeException(BECode.sms_301000);
		Sms163Helper.sendSms(3032114, phone, code);
		
//		SMSHelper.sendSMS(phone, "换绑手机验证码 ".concat(getCode(phone, SmsConstants.CHANGE_PHONE)).concat("，有效期10分钟"));
	}

	public void checkSmsCode(String phone, String code, String type) {
		Sms sms = getSMSByPhoneAndCode(phone, code, type);
		if (sms == null) 
			throw new ErrorCodeException(BECode.sms_301005);
		
		/* 验证码时间失效 30分钟 */
		if (DateUtil.getDiffeMinute(sms.getGmtSend()) > 30) 
			throw new ErrorCodeException(BECode.sms_301004);
		
		/* 将时间设置为失效时间 */
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -1);
		sms.setGmtSend(cal.getTime());
		
		smsDao.update(sms);
	}

	public Sms getSMSByPhoneAndType(String phone, String type) {
		Sms sms = new Sms();
		sms.setPhone(phone);
		sms.setType(type);
		
		return (Sms) smsDao.query(sms);
	}
	
	/**
	 * 返回验证码
	 * @param phone
	 * @param type
	 * @return
	 */
	private String getCode(String phone, String type) {
		Sms sms = getSMSByPhone(phone, type);
		
		String _code = String.valueOf((int)((Math.random()*9+1)*1000));
		
		/* 如果不存在，则直接新增 */
		if (sms == null) {
			saveSMS(phone, _code, 1, "create", type);
			
			return _code;
		}
		
		/* 当重发短信时间在1分钟以内的话，则提示发送频繁 */
		if (DateUtil.getDiffeSeconds(sms.getGmtSend()) <=30)
			throw new ErrorCodeException(BECode.sms_301002);
		
		/* 一天内超过数超过6次时，则提示警告 */
		if (DateUtil.getDiffeDay(sms.getGmtSend()) == 0 && sms.getLimitNumber() > 6) 
			throw new ErrorCodeException(BECode.sms_301003);
		
		/* 次日发送时，则重新计算数次 */
		if (DateUtil.getDiffeDay(sms.getGmtSend()) > 0) {
			saveSMS(phone, _code, 1, "update", type);
			
			return _code;
		}
		
		/* 如果在10分钟内，则发送相同短信码 */
		if (DateUtil.getDiffeMinute(sms.getGmtSend()) < 30) {
			
			saveSMS(phone, sms.getCode(), sms.getLimitNumber()+1, "update", type);
			
			return sms.getCode();
		}
		
		saveSMS(phone, _code, sms.getLimitNumber()+1, "update", type);
		
		return _code;
	}
	
	/**
	 * 保存验证码
	 * @param phone
	 * @param code
	 * @param limitnumber
	 * @param status
	 * @param type
	 */
	private void saveSMS(String phone, String code, int limitnumber, String status, String type) {
		Sms sms = new Sms();
		sms.setPhone(phone);
		sms.setCode(code);
		sms.setLimitNumber(limitnumber);
		sms.setGmtSend(new Date());
		sms.setType(type);
		
		if ("update".equals(status)) {
			smsDao.update(sms);
		} else {
			smsDao.insert(sms);
		}
	}
	
	/**
	 * 根据手机号码和类型，返回短信信息
	 * @param phone
	 * @param type
	 * @return
	 */
	private Sms getSMSByPhone(String phone, String type) {
		
		return getSMSByPhoneAndCode(phone, null, type);
	}
	
	/**
	 * 发送计划提醒
	 * @param phone
	 */
	public void sendPlanNotice(String phone){
		Sms163Helper.sendSms(6280, phone, "");
	}
	
	/**
	 * 重置密码
	 * @param phone
	 * @param passwd 密码
	 */
	public void resetpasswd(String phone,String passwd) {
		if(StringUtil.isEmpty(passwd))
			throw new ErrorCodeException(BECode.sms_301000);
		Sms163Helper.sendSms(3032113, phone, passwd);
//		SMSHelper.sendSMS(phone, "注册验证码 ".concat(getCode(phone, SmsConstants.REGISTER_TYPE)).concat("，有效期10分钟"));
	}
	
	/**
	 * 根据手机号码、验证码、类型，返回短信信息
	 * @param phone
	 * @param code
	 * @param type
	 * @return
	 */
	private Sms getSMSByPhoneAndCode(String phone, String code, String type) {
		Sms sms = new Sms();
		sms.setPhone(phone);
		sms.setCode(code);
		sms.setType(type);
		
		return (Sms) smsDao.query(sms);
	}
	
}
